package org.dragonnova.meetingserver.server.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;

import org.dragonnova.meetingserver.utils.Constants;
import org.dragonnova.meetingserver.utils.LogUtil;

public class MediaButtonReceiver extends BroadcastReceiver {

    private final String TAG = MediaButtonReceiver.class.getName();
    private Intent clickIntent = new Intent(Constants.MEDIA_BUTTON_BROADCAST_ACTION);
    public MediaButtonReceiver() {
        super();
        LogUtil.e(TAG, "created");
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String intentAction = intent.getAction();
        KeyEvent keyEvent = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        LogUtil.e(TAG, "Action=" + intentAction + "  KeyEvent=" + keyEvent.toString());
        int keyAction = keyEvent.getAction();
        if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction) && KeyEvent.ACTION_DOWN == keyAction) {
            int keyCode = keyEvent.getKeyCode();
            if (KeyEvent.KEYCODE_MEDIA_NEXT == keyCode) {
                LogUtil.e(TAG, "KEYCODE_MEDIA_NEXT");
            }
            if (KeyEvent.KEYCODE_MEDIA_PREVIOUS == keyCode) {
                LogUtil.e(TAG, "KEYCODE_MEDIA_PREVIOUS");
            }
            if (KeyEvent.KEYCODE_HEADSETHOOK == keyCode) {
                LogUtil.e(TAG, "KEYCODE_HEADSETHOOK");
                context.sendBroadcast(clickIntent);
            }
        } else if (KeyEvent.ACTION_UP == keyAction) {
            LogUtil.e(TAG, "KEYUP");
        }
    }
}
